home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / MacPerl Source ƒ / Perl5 / ext / MacPerl / OSA.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-24  |  3.2 KB  |  151 lines  |  [TEXT/MPS ]

  1. /* $Header: tyrathect:Development:Perl::RCS:missing.c,v 1.2 1994/05/04 02:12:43 neeri Exp $
  2.  *
  3.  *    Copyright (c) 1995 Matthias Neeracher
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log: missing.c,v $
  9.  */
  10.  
  11. #define MAC_CONTEXT
  12.  
  13. #include "EXTERN.h"
  14. #include "perl.h"
  15. #include "XSUB.h"
  16. #include <Types.h>
  17. #include <Components.h>
  18. #include <AppleEvents.h>
  19. #include <AppleScript.h>
  20. #include <OSA.h>
  21. #include <Gestalt.h>
  22.  
  23. ComponentInstance gScriptingComponent;
  24.  
  25. void ShutDownAppleScript(void)
  26. {
  27.     CloseComponent(gScriptingComponent);
  28.     
  29.     gScriptingComponent = nil;
  30. }
  31.  
  32. OSErr InitAppleScript(void)
  33. {
  34.     OSErr                myErr;
  35.     ComponentDescription descr;
  36.     ComponentDescription capabilities;
  37.     Component            myComponent;
  38.     EventRecord          myEvent;
  39.     short                retryCount;
  40.     long                        res;
  41.             
  42.     retryCount = 0;
  43.     
  44.     if (Gestalt(gestaltAppleEventsAttr, &res))
  45.         return -1;
  46.     else if (!(res & (1<<gestaltAppleEventsPresent)))
  47.         return -1;
  48.     else if (Gestalt(gestaltComponentMgr, &res))
  49.         return -1;
  50.         
  51.     descr.componentType         = kOSAComponentType;
  52.     descr.componentSubType      = kAppleScriptSubtype;
  53.     descr.componentManufacturer = (OSType) 0;
  54.     descr.componentFlags        = kOSASupportsCompiling + 
  55.                                             kOSASupportsGetSource + 
  56.                                             kOSASupportsAESending;
  57.     descr.componentFlagsMask    = descr.componentFlags;
  58.     
  59.     myComponent = FindNextComponent(nil, &descr);
  60.     
  61.     if (myComponent==nil)
  62.           return -1;
  63.     else {
  64.         myErr = GetComponentInfo(myComponent, &capabilities, nil, nil, nil);
  65.         gScriptingComponent = OpenComponent(myComponent);
  66.         if (!gScriptingComponent)
  67.             return(-1);
  68.         else
  69.             atexit(ShutDownAppleScript);
  70.     }
  71.         
  72.     return myErr;
  73. }
  74.  
  75. XS(XS_MacPerl_MP_Reply)
  76. {
  77.     dXSARGS;
  78.     if (items != 1) {
  79.     croak("Usage: MacPerl::Reply(reply)");
  80.     }
  81.     {
  82.     char *    reply = (char *)SvPV(ST(0),na);
  83.     {
  84.         if (gPerlReply)
  85.             DisposeHandle(gPerlReply);
  86.     /**/        
  87.         PtrToHand(reply, &gPerlReply, strlen(reply));
  88.     }
  89.     }
  90.     XSRETURN(1);
  91. }
  92.  
  93. XS(XS_MacPerl_MP_DoAppleScript)
  94. {
  95.     dXSARGS;
  96.     if (items != 1) {
  97.     croak("Usage: MacPerl::DoAppleScript(script)");
  98.     }
  99.     {
  100.     SV *    script = ST(0);
  101.     {
  102.         AEDesc    source;
  103.         AEDesc    result;
  104.         char *    scriptText;
  105.         STRLEN    len;
  106.     /**/        
  107.         if (!gScriptingComponent && InitAppleScript())
  108.             croak("MacPerl::DoAppleScript couldn't initialize AppleScript");
  109.     /**/        
  110.         scriptText = (char*) SvPV(ST(0), len);
  111.         AECreateDesc(typeChar, scriptText, len, &source);
  112.     /**/        
  113.         if (!OSADoScript(
  114.                 gScriptingComponent, 
  115.                 &source, 
  116.                 kOSANullScript, 
  117.                 typeChar, 
  118.                 kOSAModeCanInteract,
  119.                 &result))
  120.         {
  121.             AEDisposeDesc(&source);
  122.     /**/        
  123.             if (!AECoerceDesc(&result, typeChar, &source)) {
  124.                 HLock(source.dataHandle);
  125.                 ST(0) = sv_2mortal(newSVpv(*source.dataHandle,GetHandleSize(source.dataHandle)));
  126.                 AEDisposeDesc(&source);
  127.             } else
  128.                 ST(0) = &sv_undef;
  129.     /**/        
  130.             AEDisposeDesc(&result);
  131.         } else {
  132.             AEDisposeDesc(&source);
  133.     /**/        
  134.             ST(0) = &sv_undef;
  135.         }
  136.     }
  137.     }
  138.     XSRETURN(1);
  139. }
  140.  
  141. XS(boot_OSA)
  142. {
  143.     dXSARGS;
  144.     char* file = __FILE__;
  145.  
  146.     newXS("MacPerl::Reply", XS_MacPerl_MP_Reply, file);
  147.     newXS("MacPerl::DoAppleScript", XS_MacPerl_MP_DoAppleScript, file);
  148.     ST(0) = &sv_yes;
  149.     XSRETURN(1);
  150. }
  151.